package com.voe;

import com.voe.common.NetworkStatistics;
import com.voe.common.OutputParam;
import com.voe.common.ReturnValue;

public class VoENetEqStats {
	
	// [start] apis

	public static VoENetEqStats createInstance(VoiceEngine voiceEngine) {

		long p = GetInterface(voiceEngine._p);
		if(p != 0)
			return new VoENetEqStats(p);
		return null;
	}

	public void release() {
		if (_p != 0) {
			Release(_p);
		}
	}
	
	/** Get the "in-call" statistics from NetEQ.<br/>
	 *  The statistics are reset after the query.
	 */
	public int getNetworkStatistics(int channel, ReturnValue<NetworkStatistics> stats) {
		OutputParam output = new OutputParam();
		int result = GetNetworkStatistics(_p, channel, output);
		if(result == 0) {
			NetworkStatistics tmp = new NetworkStatistics();
			tmp.currentBufferSize = ((Integer)output.getObject()[0]).shortValue();
			tmp.preferredBufferSize = ((Integer)output.getObject()[1]).shortValue();
			tmp.jitterPeaksFound = (Boolean)output.getObject()[2];
			tmp.currentPacketLossRate = ((Integer)output.getObject()[3]).shortValue();
			tmp.currentDiscardRate = ((Integer)output.getObject()[4]).shortValue();
			tmp.currentExpandRate = ((Integer)output.getObject()[5]).shortValue();
			tmp.currentPreemptiveRate = ((Integer)output.getObject()[6]).shortValue();
			tmp.currentAccelerateRate = ((Integer)output.getObject()[7]).shortValue();
			tmp.clockDriftPPM = (Integer)output.getObject()[8];
			tmp.meanWaitingTimeMs = (Integer)output.getObject()[9];
			tmp.medianWaitingTimeMs = (Integer)output.getObject()[10];
			tmp.minWaitingTimeMs = (Integer)output.getObject()[11];
			tmp.maxWaitingTimeMs = (Integer)output.getObject()[12];
			tmp.addedSamples = (Integer)output.getObject()[13];
			stats.setValue(tmp);
		}
		return result;
	}

	protected VoENetEqStats(long p) {
		_p = p;
	}

	protected long _p;
	
	// [end]
	
	
	
	
	

	// [start] jni api

	// Factory for the VoENetEqStats sub-API. Increases an internal
	// reference counter if successful. Returns NULL if the API is not
	// supported or if construction fails.
	private static native long GetInterface(long p_voiceEngine);

	// Releases the VoENetEqStats sub-API and decreases an internal
	// reference counter. Returns the new reference count. This value should
	// be zero for all sub-API:s before the VoiceEngine object can be safely
	// deleted.
	private static native int Release(long p_VoENetEqStats);

	// Get the "in-call" statistics from NetEQ.
	// The statistics are reset after the query.
	private static native int GetNetworkStatistics(long p_VoENetEqStats,
			int channel, OutputParam stats);

	// [end]
}
